import React from 'react';
import { Form, Input, Button, message } from 'antd';
import './style.css'
import axios from "axios";
import qs from 'qs';

interface IState{
    isLogin:boolean
}
interface formData{
    password:string
}

class Login extends React.Component<any, IState>{
    state={
        isLogin:false
    }
    onFinish = (values: formData) => {
        console.log('Success:', values);
        axios.post('/api/login',qs.stringify({
            password:values.password
        }),{
            headers:{
                "Content-Type":'application/x-www-form-urlencoded'
            }
        }).then(res=>{
            if(res.data?.data){
                this.setState({
                    isLogin:true
                })
            }else{
                message.error("登陆失败")
            }
        })
    };

    onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);
    };
    render() {

        if(this.state.isLogin) {
            window.location.href='/'
            return '';
        }
        return (
            <div className='login-page-div'>
                <Form
                    name="basic"
                    labelCol={{ span: 8 }}
                    wrapperCol={{ span: 16 }}
                    initialValues={{ remember: true }}
                    onFinish={this.onFinish}
                    onFinishFailed={this.onFinishFailed}
                    autoComplete="off"
                >
                    <Form.Item
                        label="Password"
                        name="password"
                        rules={[{ required: true, message: '请输入登陆密码！' }]}
                    >
                        <Input.Password />
                    </Form.Item>

                    <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                        <Button type="primary" htmlType="submit">
                            Submit
                        </Button>
                    </Form.Item>
                </Form>
            </div>

        );;
    }
}

export default Login;